﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmThuChiTongHopThuQuy : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private NhanVienBOList nhanVienBOList = null;
        #endregion variant

        //=====================================
        #region form
        public FrmThuChiTongHopThuQuy()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmThuChiTongHopThuQuy_Load);
            this.Shown += new EventHandler(FrmThuChiTongHopThuQuy_Shown);
            this.KeyDown += new KeyEventHandler(FrmThuChiTongHopThuQuy_KeyDown);

            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.btnPrint.ArrowButtonClick += new EventHandler(btnPrint_ArrowButtonClick);
            this.btnPrint.Click += new EventHandler(btnInTonghop_Click);

            this.btnIn1Phieu.Click += new EventHandler(btnIn1Phieu_Click);
            this.btnInTatCa.Click += new EventHandler(btnInTatCa_Click);
            this.btnInTonghop.Click += new EventHandler(btnInTonghop_Click);
        }

        private void FrmThuChiTongHopThuQuy_Shown(object sender, EventArgs e)
        {
            this.cmbLoaiTongHop.Focus();
        }

        private void FrmThuChiTongHopThuQuy_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();
            else
                if (this.btnRefresh.Visible && ((e.KeyCode == Keys.R && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F5)))
                    RefreshData();
                else
                    if (this.btnPrint.Visible && ((e.KeyCode == Keys.P && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F7)))
                        InTongHop();
                    else
                        if ((e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F3))
                            FilterData();
        }

        private void FrmThuChiTongHopThuQuy_Load(object sender, EventArgs e)
        {
            DateTime dtNow = DateTime.Now;
            this.dtTuNgay.DateTime = new DateTime(dtNow.Year, dtNow.Month, 1);
            this.dtDenNgay.DateTime = dtNow;

            MyStyle.SetGridStyle(this.grdView, false);

            RefreshData();

            this.chkBoPhan.CheckedChanged += new EventHandler(chk_CheckedChanged);
            this.chkQuyenSo.CheckedChanged += new EventHandler(chk_CheckedChanged);
            this.chkSo.CheckedChanged += new EventHandler(chk_CheckedChanged);
            this.chkNo.CheckedChanged += new EventHandler(chk_CheckedChanged);
            this.chkCo.CheckedChanged += new EventHandler(chk_CheckedChanged);
            this.chkSoTienTu.CheckedChanged += new EventHandler(chk_CheckedChanged);
            this.chkNguoi.CheckedChanged += new EventHandler(chk_CheckedChanged);

            this.grdView.CustomDrawGroupRow += new RowObjectCustomDrawEventHandler(grdView_CustomDrawGroupRow);
            this.btnSearch.Click += new EventHandler(btnSearch_Click);
        }
        #endregion form

        //=====================================
        #region control
        private void chk_CheckedChanged(object sender, EventArgs e)
        {
            CheckEdit chk = (CheckEdit)sender;
            bool enable = chk.Checked;
            switch (chk.Name)
            {
                case "chkBoPhan":
                    this.cmbBoPhan.Enabled = enable;
                    break;
                case "chkQuyenSo":
                    this.cmbQuyenSo.Enabled = enable;
                    break;
                case "chkSo":
                    this.cmbSo.Enabled = enable;
                    break;
                case "chkNo":
                    this.cmbNo.Enabled = enable;
                    break;
                case "chkCo":
                    this.cmbCo.Enabled = enable;
                    break;
                case "chkSoTienTu":
                    this.numSoTienTu.Enabled = enable;
                    this.numSoTienDen.Enabled = enable;
                    break;
                case "chkNguoi":
                    this.cmbNguoi.Enabled = enable;
                    break;
            }
        }
        #endregion control

        //=====================================
        #region button
        private void btnSearch_Click(object sender, EventArgs e)
        {
            FilterData();
        }

        private void btnPrint_ArrowButtonClick(object sender, EventArgs e)
        {
            this.contextMenuStrip1.Show(this.pnFooter, new Point(this.btnPrint.Location.X, this.btnPrint.Location.Y - 80));
        }

        private void btnIn1Phieu_Click(object sender, EventArgs e)
        {
            In1Phieu();
        }

        private void btnInTonghop_Click(object sender, EventArgs e)
        {
            InTongHop();
        }

        private void btnInTatCa_Click(object sender, EventArgs e)
        {
            InTatCa();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        private void grdView_CustomDrawGroupRow(object sender, RowObjectCustomDrawEventArgs e)
        {
            if (this.cmbLoaiTongHop.SelectedIndex == 2)
            {
                GridGroupRowInfo info = e.Info as GridGroupRowInfo;

                if (info.Column.FieldName == "LoaiPhieu")
                {
                    int LoaiPhieu = StaticUtil.GetInt(info.EditValue);
                    if (LoaiPhieu == 1)
                        info.GroupText = "Phiếu thu";
                    else
                        info.GroupText = "Phiếu chi";
                }
            }
        }
        #endregion grid

        //=====================================
        #region function
        private void FilterData()
        {
            this.colLoaiPhieu.GroupIndex = (this.cmbLoaiTongHop.SelectedIndex == 2 ? 0 : -1);
            this.colSTT0_ThuChi.Width = (this.cmbLoaiTongHop.SelectedIndex == 2 ? 60 : 35);

            #region lay dieu kien
            int LoaiTongHop = this.cmbLoaiTongHop.SelectedIndex;
            LoaiTongHop = (LoaiTongHop == 0 ? 1 : LoaiTongHop == 1 ? 0 : 2);

            DateTime dtTuNgay = this.dtTuNgay.DateTime;
            DateTime dtDenNgay = this.dtDenNgay.DateTime;

            if (dtTuNgay > dtDenNgay)
            {
                dtTuNgay = dtDenNgay;
                dtDenNgay = this.dtTuNgay.DateTime;
            }

            int ITuNgay = dtTuNgay.Year * 10000 + dtTuNgay.Month * 100 + dtTuNgay.Day;
            int IDenNgay = dtDenNgay.Year * 10000 + dtDenNgay.Month * 100 + dtDenNgay.Day;

            string BoPhan = null;
            if (this.chkBoPhan.Checked)
                if (String.IsNullOrEmpty(this.cmbBoPhan.Text.Trim()) == false)
                    BoPhan = this.cmbBoPhan.Text;

            string QuyenSo = null;
            if (this.chkQuyenSo.Checked)
                if (String.IsNullOrEmpty(this.cmbQuyenSo.Text.Trim()) == false)
                    QuyenSo = this.cmbQuyenSo.Text;

            string So = null;
            if (this.chkSo.Checked)
                if (String.IsNullOrEmpty(this.cmbSo.Text.Trim()) == false)
                    So = this.cmbSo.Text;

            string No = null;
            if (this.chkNo.Checked)
                if (String.IsNullOrEmpty(this.cmbNo.Text.Trim()) == false)
                    No = this.cmbNo.Text;

            string Co = null;
            if (this.chkCo.Checked)
                if (String.IsNullOrEmpty(this.cmbCo.Text.Trim()) == false)
                    Co = this.cmbCo.Text;

            double? SoTienTu = null;
            if (this.chkSoTienTu.Checked)
                SoTienTu = decimal.ToDouble(this.numSoTienTu.Value);

            double? SoTienDen = null;
            if (SoTienTu != null)
            {
                SoTienDen = decimal.ToDouble(this.numSoTienDen.Value);

                if (SoTienTu < SoTienDen)
                {
                    SoTienTu = SoTienDen;
                    SoTienDen = decimal.ToDouble(this.numSoTienTu.Value);
                }
            }

            string Nguoi = null;
            if (this.chkNguoi.Checked)
                if (String.IsNullOrEmpty(this.cmbNguoi.Text.Trim()) == false)
                    Nguoi = this.cmbNguoi.Text;
            #endregion lay dieu kien

            string where = "INgay>=" + ITuNgay + " AND INgay<=" + IDenNgay;
            if (LoaiTongHop < 2)
                where += " AND LoaiPhieu=" + LoaiTongHop.ToString();
            if (BoPhan != null)
                where += " AND BoPhan='" + BoPhan + "'";
            if (QuyenSo != null)
                where += " AND QuyenSo='" + QuyenSo + "'";
            if (So != null)
                where += " AND So='" + So + "'";
            if (No != null)
                where += " AND No='" + No + "'";
            if (Co != null)
                where += " AND Co='" + Co + "'";
            if (SoTienTu != null && SoTienDen != null)
                where += " AND SoTien>=" + SoTienTu.Value + " AND SoTien<=" + SoTienDen.Value;
            if (Nguoi != null)
                where += " AND Nguoi='" + Nguoi + "'";

            this.grdView.ActiveFilterString = where;
            this.grdView.CheckLoaded();
            SetStt();
        }

        private void RefreshData()
        {
            this.nhanVienBOList = NhanVienBOList.GetNhanVienBOList();

            ThuChiThuQuyReduceBOCollection thuChiThuQuyBOCollection = ThuChiThuQuyReduceBOCollection.GetThuChiThuQuyReduceBOCollection("", "NgayLapPhieu");
            this.bdSourceThuChi.DataSource = thuChiThuQuyBOCollection;

            //-------------
            this.cmbBoPhan.Properties.Items.Clear();
            foreach (string BoPhan in thuChiThuQuyBOCollection.hstBoPhan.Keys)
                this.cmbBoPhan.Properties.Items.Add(BoPhan);

            if (this.cmbBoPhan.Properties.Items.Count > 0)
                this.cmbBoPhan.SelectedIndex = 0;

            //-------------
            this.cmbNguoi.Properties.Items.Clear();
            foreach (string Nguoi in thuChiThuQuyBOCollection.hstNguoi.Keys)
                this.cmbNguoi.Properties.Items.Add(Nguoi);

            if (this.cmbNguoi.Properties.Items.Count > 0)
                this.cmbNguoi.SelectedIndex = 0;

            //-------------
            this.cmbQuyenSo.Properties.Items.Clear();
            foreach (string QuyenSo in thuChiThuQuyBOCollection.hstQuyenSo.Keys)
                this.cmbQuyenSo.Properties.Items.Add(QuyenSo);

            if (this.cmbQuyenSo.Properties.Items.Count > 0)
                this.cmbQuyenSo.SelectedIndex = 0;

            //-------------
            this.cmbSo.Properties.Items.Clear();
            foreach (string So in thuChiThuQuyBOCollection.hstSo.Keys)
                this.cmbSo.Properties.Items.Add(So);

            if (this.cmbSo.Properties.Items.Count > 0)
                this.cmbSo.SelectedIndex = 0;

            //-------------
            this.cmbNo.Properties.Items.Clear();
            foreach (string No in thuChiThuQuyBOCollection.hstNo.Keys)
                this.cmbNo.Properties.Items.Add(No);

            if (this.cmbNo.Properties.Items.Count > 0)
                this.cmbNo.SelectedIndex = 0;

            //-------------
            this.cmbCo.Properties.Items.Clear();
            foreach (string Co in thuChiThuQuyBOCollection.hstCo.Keys)
                this.cmbCo.Properties.Items.Add(Co);

            if (this.cmbCo.Properties.Items.Count > 0)
                this.cmbCo.SelectedIndex = 0;


            FilterData();
        }

        private void SetStt()
        {
            int stt = 0;
            int length = this.grdView.RowCount;
            int index = 0;
            for (int i = 0; i < length; i++)
            {
                index = this.grdView.GetVisibleRowHandle(i);
                if (index >= 0)
                    this.grdView.SetRowCellValue(index, "STT0", ++stt);
            }
        }

        private void In1Phieu()
        {
            int? LoaiPhieu = StaticUtil.GetRealInt(this.grdView.GetFocusedRowCellValue("LoaiPhieu"));
            int? ThuChiID = StaticUtil.GetRealInt(this.grdView.GetFocusedRowCellValue("ThuChiThuQuyID"));
            if (LoaiPhieu != null)
            {
                if (ThuChiID != null)
                {
                    ThuChiThuQuyReduceBO thuChiBO = ((ThuChiThuQuyReduceBOCollection)this.bdSourceThuChi.DataSource).GetThuChiThuQuyReduceBO(ThuChiID.Value);

                    if (LoaiPhieu.Value == 1)
                    {
                        //goi phieu thu
                        Report.rpPhieuThu report = new TanHungLong.Report.rpPhieuThu(thuChiBO, this.nhanVienBOList);
                        report.ShowPreviewDialog();
                    }
                    else
                    {
                        //goi phieu chi
                        Report.rpPhieuChi report = new TanHungLong.Report.rpPhieuChi(thuChiBO, this.nhanVienBOList);
                        report.ShowPreviewDialog();
                    }
                }
            }
        }

        private void InTatCa()
        {
            int index = 0;
            int length = this.grdView.RowCount;

            if (length > 0)
            {
                ThuChiThuQuyReduceBOCollection thuChiBOCollection = ((ThuChiThuQuyReduceBOCollection)this.bdSourceThuChi.DataSource);
                ThuChiThuQuyReduceBOCollection tmpThuChiBOCollection = ThuChiThuQuyReduceBOCollection.NewThuChiThuQuyReduceBOCollection();

                for (int i = 0; i < length; i++)
                {
                    index = this.grdView.GetVisibleRowHandle(i);
                    if (index >= 0)
                        tmpThuChiBOCollection.Add(thuChiBOCollection[this.grdView.GetDataSourceRowIndex(index)]);
                }

                Report.rpDanhSachThuChi report = new TanHungLong.Report.rpDanhSachThuChi(tmpThuChiBOCollection, this.nhanVienBOList);
                report.ShowPreviewDialog();
            }
        }

        private void InTongHop()
        {
            int index = 0;
            int length = this.grdView.RowCount;

            if (length > 0)
            {
                ThuChiThuQuyReduceBOCollection thuChiBOCollection = ((ThuChiThuQuyReduceBOCollection)this.bdSourceThuChi.DataSource);
                ThuChiThuQuyReduceBOCollection tmpThuChiBOCollection = ThuChiThuQuyReduceBOCollection.NewThuChiThuQuyReduceBOCollection();

                for (int i = 0; i < length; i++)
                {
                    index = this.grdView.GetVisibleRowHandle(i);
                    if (index >= 0)
                        tmpThuChiBOCollection.Add(thuChiBOCollection[this.grdView.GetDataSourceRowIndex(index)]);
                }

                int LoaiTongHop = this.cmbLoaiTongHop.SelectedIndex;
                Report.rpTongHopThuChiThuQuy report = new TanHungLong.Report.rpTongHopThuChiThuQuy((LoaiTongHop == 0 ? "PHIẾU THU" : LoaiTongHop == 1 ? "PHIẾU CHI" : "THU CHI"), tmpThuChiBOCollection);
                report.ShowPreviewDialog();
            }
        }
        #endregion function
    }
}